package top.roadvast.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import top.roadvast.activiti.pojo.Holiday;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author 郭泽鹏
 * @Date 2020/6/23 9:04
 * @Version 1.0
 * 启动流程实例(前提：已经完成流程定义的部署工作)
 * 影响到的表：
 * act_hi_actinst：已完成的活动信息
 * act_hi_identitylink：参与者信息
 * act_hi_procinst：流程实例
 * act_hi_taskinst：任务实例
 * act_ru_execution：执行表
 * act_ru_identitylink：参与者信息
 * act_ru_task：任务
 *
 *
 */
public class ActivitiStartInstance {
    @Test
    public void testBasic() {
        //1、得到 ProcessEngine 对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2、得到 RuntimeService 对象
        RuntimeService runtimeService = processEngine.getRuntimeService();

        //3、创建流程实例，参数：流程定义的key
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holiday");
        final ProcessInstance processInstance = runtimeService.startProcessInstanceById("holiday-listener:1:82504");
        //4、输出实例相关信息
        System.out.println("流程部署ID：" + processInstance.getDeploymentId());  //null
        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());   //holiday:7:15004
        System.out.println("流程实例ID：" + processInstance.getId());    //20001
        System.out.println("活动ID：" + processInstance.getActivityId());  //null
    }

    @Test
    public void testUel() {
        //1、得到 ProcessEngine 对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2、得到 RuntimeService 对象
        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map<String, Object> map = new HashMap<>();
        map.put("assigner0", "lilei");
        map.put("assigner1", "hanmeimei");
        map.put("assigner2", "changqin");

        //3、创建流程实例，参数：流程定义的key
        ProcessInstance processInstance = runtimeService.startProcessInstanceById("holiday-uel:2:72504", map);

        //4、输出实例相关信息
        System.out.println("流程部署ID：" + processInstance.getDeploymentId());  //null
        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());   //holiday:7:15004
        System.out.println("流程实例ID：" + processInstance.getId());    //20001
        System.out.println("活动ID：" + processInstance.getActivityId());  //null
    }

    @Test
    public void testVariable() {
        //1、得到 ProcessEngine 对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2、得到 RuntimeService 对象
        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map<String, Object> map = new HashMap<>();
        map.put("assigner0", "zhangsan");
        map.put("assigners0", "lisi,wangwu");
//        map.put("assigner2", "wangwu");
//        final Holiday holiday = new Holiday(4D);
//        map.put("holiday", holiday);

        //3、创建流程实例，参数：流程定义的key
        ProcessInstance processInstance = runtimeService.startProcessInstanceById("holiday-group:1:4", map);

        //4、输出实例相关信息
        System.out.println("流程部署ID：" + processInstance.getDeploymentId());  //null
        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());   //holiday:7:15004
        System.out.println("流程实例ID：" + processInstance.getId());    //20001
        System.out.println("活动ID：" + processInstance.getActivityId());  //null
    }
}